import { useEffect, useState } from 'react';

function useDebounce(value: any, delay = 300) {
  const [debounceValue, setDebounceValue] = useState(value);
  useEffect(() => {
    const handler = window.setTimeout(() => {
      setDebounceValue(value);
    }, delay);
    console.log('=========value==================');
    console.log('变化了');
    console.log('===========delay==================');
    return () => {
      window.clearTimeout(handler);
    };
  }, [value, delay]);

  return debounceValue;
}

// 这个 effect 会在 value 和 delay 变化的时候执行
export default useDebounce;
